<?php
    session_start();
    if(!isset($_SESSION['admin_login']) || $_SESSION['admin_login'] != 1)
    {
        header("Location: ../");
    }
    
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manager Export</title>
<script src="../js/jquery-1.10.0.js">
</script>
</head>

<body>
<center>
<label for="type">Chọn Bảng Muốn Xuất : </label>
<select id="type">
<option value="student">Sinh Viên</option>
<option value="teacher">Giáo Viên</option>
<option value="manager">Quản Trị Viên</option>
<option value="subject">Môn Học</option>
<option value="department">Khoa</option>
<option value="class">Lớp</option>
<option value="register">Đăng Kí</option>
<option value="result">Kết Quả Học Tập</option>
<option value="semeter">Học Kì</option>
</select>
<br />
<br />
<img src=""/>
<br />
<br />
<label for="chooseColumn" id="label">Chọn Cột Muốn Xuất : </label>
<br />
<br />
<div id="chooseColumn">
</div>
<br />
<center>
<label for="fileType1">Excel</label>
<input type="radio" name="fileType" id="fileType1" value="xlsx" checked="checked" />
<br />
<label for="fileType1">PDF</label>
<input type="radio" name="fileType" id="fileType2" value="pdf"/>
<br />
<br />
<input type='button' value='Export' name='export' id='export'>
</center>
<br />
<br />
<div id="review">
</div>
</center>
<!-------------------------------------------------------------------------------------------------------------->
<script>
$(document).ready(function(e){
	var checkedArr;
	$.ajax({
			url:"ReturnManagerExportForm.php",
			data:{type:$("#type").val()}
		}).done(function(msg){
			$("#chooseColumn").html(msg);
			callMe();
			$("input[type=button]").click(function(){
				checkedArr = new Array();
				selectedArr = new Array();
				checkboxArr = new Array();
				$("img").attr("src","../image/source/loader.gif");
				$("input[type=checkbox]:checked").each(function(){	
						checkedArr.push($(this).val());	
					});
				$("input[type=checkbox]").each(function(){
						checkboxArr.push($(this).val());
					});
				$("select").each(function(){
					if($(this).attr("name"))
					{
						name = $(this).attr("name");
						name= name.split("_")[1];
						selectedArr.push($(this).val());
					}
				});
				$.ajax({
					url:"managerExportExcel.php",
					data: {fileType: $("input[type=radio]:checked").val(),table:$("#table").val(),checkboxArr: checkboxArr,checkedArr: checkedArr,selectedArr: selectedArr}
				}).done(function(msg){;
					index = msg.indexOf("exported");
					msg = msg.substr(index,msg.length);
					window.location=msg;
					$("img").attr("src","");
				});
			});
	});
	$("#type").change(function(){
		$("img").attr("src","../image/source/loader.gif");
		$("#review").html("");
		$.ajax({
			url:"ReturnManagerExportForm.php",
			data:{type:$("#type").val()}
		}).done(function(msg){
			$("#chooseColumn").html(msg);
			callMe();
			$("img").attr("src","");
		});
	});
   function callMe()
   {
	   $("input[type=checkbox]").change(function(){
		  $("#review").hide();
		  checkedArr = new Array();
		  selectedArr = new Array();
		  checkboxArr = new Array();
		  $("#review").html("");
		  $("img").attr("src","../image/source/loader.gif");
		  $("input[type=checkbox]:checked").each(function(){	
			  checkedArr.push($(this).val());	
		  });
		  $("input[type=checkbox]").each(function(){
			  checkboxArr.push($(this).val());
		  });
		  $("select").each(function(){
			  if($(this).attr("name"))
			  {
				  name = $(this).attr("name");
				  name= name.split("_")[1];
				  selectedArr.push($(this).val());
			  }
		  });
		  $.ajax({
			  url:"managerExportFilter.php",
			  data: {table:$("#table").val(),checkboxArr: checkboxArr,checkedArr: checkedArr,selectedArr: selectedArr}
		  }).done(function(msg){;
			  $("#review").html(msg);
			  $("#review").show("slow");
			  $("img").attr("src","");
		  });
	  });	
	  $("select").change(function(){
		  if($(this).attr("id")!="type")
		  {
			  $("#review").hide();
			  checkedArr = new Array();
			  selectedArr = new Array();
			  checkboxArr = new Array();
			  $("#review").html("");
			  $("img").attr("src","../image/source/loader.gif");
			  $("input[type=checkbox]:checked").each(function(){	
				  checkedArr.push($(this).val());	
			  });
			  $("input[type=checkbox]").each(function(){
				  checkboxArr.push($(this).val());
			  });
			  $("select").each(function(){
				  if($(this).attr("name"))
				  {
					  name = $(this).attr("name");
					  name= name.split("_")[1];
					  selectedArr.push($(this).val());
				  }
				  });
			  $.ajax({
				  url:"managerExportFilter.php",
				  data: {table:$("#table").val(),checkboxArr: checkboxArr,checkedArr: checkedArr,selectedArr: selectedArr}
			  }).done(function(msg){;
				  $("#review").html(msg);
				  $("#review").show("slow");
				  $("img").attr("src","");
			  });
		  }
	  });
	  $("#review").hide();
	  checkedArr = new Array();
	  selectedArr = new Array();
	  checkboxArr = new Array();
	  $("img").attr("src","../image/source/loader.gif");
	  $("input[type=checkbox]:checked").each(function(){	
			  checkedArr.push($(this).val());	
		  });
	  $("input[type=checkbox]").each(function(){
			  checkboxArr.push($(this).val());
		  });
	  $("select").each(function(){
		  if($(this).attr("name"))
	  	  {
			  name = $(this).attr("name");
			  name= name.split("_")[1];
			  selectedArr.push($(this).val());
		  }
	  });
	  $.ajax({
		  url:"managerExportFilter.php",
		  data: {table:$("#table").val(),checkboxArr: checkboxArr,checkedArr: checkedArr,selectedArr: selectedArr}
	  }).done(function(msg){;
		  $("#review").html(msg);
		  $("#review").show("slow");
		  $("img").attr("src","");
	  });
   }
});
</script>

</body>
</html>